﻿
using System;

namespace FacebookXmpp.Parse
{
	internal class XmlStartElement : XmlParseState
    {
		public XmlStartElement( DocumentBuilder sequenceBuilder )
			: base( sequenceBuilder )
		{ }

    	public XmlStartElement(XmlParseState from)
            : base(from)
        { }

        public override XmlParseState Parse(StringParser parser)
        {
            parser.SkipWhiteSpace();

            if (parser.PauseIfNearEnd(2))
                return this;

			if (parser.IsNext( "<?" ))
				return new XmlProcessingElement( this );

			if (parser.IsNext( "</" ))
				return new XmlCloseElement( this );

            // Consume first "<"
            if (!parser.IsNext("<"))
                throw new Exception("Invalid character " + parser.PeekChar() + " at start of Xmpp XmlStream");

            parser.Consume("<");

            char? c = parser.NextCharOf(' ', '>');

            if (c == null)
                return this;

            _builder.Current.Name = parser.ConsumeTo(c.Value, false);
            parser.Consume(c.Value.ToString());

            if (c.Value == '>')
                return new XmlElementInner(this);
            else
                return new XmlElementDefinition(this);

        }
    }
}